
import React from "react";
import {KgMobileCall} from "../../../utils/mobilecall/mobilecall-2.2";
import {kgShareFun, fxShareFun, ShareData, dkShareFun} from "./AppShare";
import ShareList from "./ShareList";
import ShareItem from "./ShareItem";
import ShareMask from "./ShareMask";
import wbImg from "../images/wb.png";
import zoneImg from "../images/zone.png";
import qqShareImg from "../images/QQshare.png";
import weixinShareImg from "../images/weixinShare.png";
import { boolDk } from "../../../utils/tool";
const ua = navigator.userAgent,
    MicroMessenger = new RegExp("MicroMessenger", "gi").test(ua),
    QQ = new RegExp("QQ/", "gi").test(ua),
    boolKg = KgMobileCall.isAndroid || KgMobileCall.isIOS,
    // android = ua.match(/(Android);?[\s\/]+([\d.]+)?/),
    boolFX = new RegExp("FanXing", "gi").test(ua);


export default class AppShare extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            show: this.props.show
        };
    }

    setAppleTouchIcon(picUrl) {
        let linkAppleIcon = document.querySelector("link[rel=apple-touch-icon]");
        let linkIcon = document.querySelector("link[rel=icon]");
        if (linkAppleIcon) linkAppleIcon.href = picUrl;
        if (linkIcon) linkIcon.href = picUrl;
    }

    setMetaDescription(content) {
        let metaDescription = document.querySelector("meta[name=description]");
        if (metaDescription) {
            metaDescription.content = content;
        } else {
            let meta = document.createElement("meta");
            meta.name = "description";
            meta.content = content;
            document.getElementsByTagName("head")[0].appendChild(meta);
        }
    }
    setShareMsg(props) {
        const {shareData, signCallback, sign} = props;

        let shareObject = {
            image: shareData.picUrl,
            link: shareData.linkUrl,
            desc: shareData.content,
            title: shareData.title
        };

        //客户端读取的全局变量参数
        window.fxShareConfig = {
            shareList: [
                "onShareWechat",
                "onShareTimeline",
                "onShareQQ",
                "onShareQzone",
                "onShareSina"
            ],
            onShareWechat: shareObject,
            onShareTimeline: shareObject,
            onShareQQ: shareObject,
            onShareQzone: shareObject,
            onShareSina: shareObject
        };

        // 进入页面初始化分享文案
        this.setAppleTouchIcon(shareData.picUrl);
        this.setMetaDescription(shareData.content);

        if( this.state.show ){
            if (boolKg) {
                kgShareFun(shareData);
            } else if (boolFX) {
                fxShareFun(shareData, sign);
            } else if (boolDk) {
                dkShareFun(shareData)
            }
        }
    }

    componentDidMount() {
        this.setShareMsg(this.props);
    }

    shouldComponentUpdate(nextProps, nextState) {
        if (
            this.props.shareData.title !== nextProps.shareData.title ||
            this.props.shareData.linkUrl !== nextProps.shareData.linkUrl ||
            this.props.shareData.picUrl !== nextProps.shareData.picUrl ||
            this.props.shareData.content !== nextProps.shareData.content ||
            this.props.shareData.summary !== nextProps.shareData.summary
        ) {
            this.setShareMsg(nextProps);
        }

        return this.state.show !== nextState.show;
    }

    componentWillReceiveProps(nextProps) {
        const {shareData, sign} = this.props;

        if (this.props.show == nextProps.show || !nextProps.show) {
            return;
        }
        this.setState({show: nextProps.show});
        if (nextProps.show) {
            if (this.props.beforeFun) {
                this.props.beforeFun();
            }
        }

        if (boolKg) {
            kgShareFun(shareData);
        } else if (boolFX) {
            fxShareFun(shareData, sign);
        } else if (boolDk) {
            dkShareFun(shareData);
        }

        //为了避免APP只能调用一次，1秒后自动调用关闭方法
        if (boolKg || boolFX || boolDk) {
            setTimeout(() => {
                this.closeFun();
            }, 1000);
        }
    }

    render() {

        let shareHtml = null;
        if (QQ || MicroMessenger) {
            const shareMaskImg = this.setImg();

            shareHtml = (
                <ShareMask
                    show={this.state.show}
                    shareMaskImg={shareMaskImg}
                    closeFun={this.closeFun.bind(this)}
                />
            );
        } else if (!boolFX && !boolKg && !boolDk) {

            shareHtml = '';
        }
        return <div>{shareHtml}</div>;
    }

    setImg() {
        const {maskImg} = this.props;
        let shareMaskImg = "";
        if (maskImg) {
            shareMaskImg = maskImg;
        } else if (QQ) {
            shareMaskImg = qqShareImg;
        } else {
            shareMaskImg = weixinShareImg;
        }
        return shareMaskImg;
    }

    shareFun(e) {
        e.stopPropagation();
        const {shareData} = this.props;
        let type = e.currentTarget.getAttribute("data-share-type");
        let url, param;
        switch (type) {
            case "zone":
                url = "http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey";
                param = [
                    "url=" + encodeURIComponent(shareData.linkUrl || window.location.href),
                    "title=" + encodeURIComponent(shareData.title || ""),
                    "desc=" + encodeURIComponent(shareData.content || ""),
                    "summary=" + encodeURIComponent(shareData.summary || ""),
                    "pics=" + encodeURIComponent(shareData.picUrl)
                ].join("&");
                break;
            case "wb":
                url = "http://service.weibo.com/share/share.php";
                param = [
                    "url=" + encodeURIComponent(shareData.linkUrl || window.location.href),
                    "title=" + encodeURIComponent(shareData.content || ""),
                    "summary=" + encodeURIComponent(shareData.summary || ""),
                    "pic=" + encodeURIComponent(shareData.picUrl)
                ].join("&");
                break;
            default: break;
        }
        if (url && param) {
            window.open(url + "?" + param, "_new");
        }
    }

    closeFun(e) {
        if (e) {
            e.stopPropagation();
        }
        if (this.props.afterFun) {
            this.props.afterFun();
        }
        this.setState({
            show: false
        });
        if (this.props.closeFun) {
            this.props.closeFun();
        }
    }
}
